<?php

namespace app\admin\controller;

use app\api\controller\Myshopify;
use app\common\controller\Backend;
use think\Db;
use think\Cache;
use fast\Tree;
/**
 * Tiktok Ads月汇总管理
 *
 * @icon fa fa-circle-o
 */
class FeditionReportdaya extends Backend
{
    
    /**
     * FeditionReportdaya模型对象
     * @var \app\admin\model\FeditionReportdaya
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\FeditionReportdaya;
        $this->view->assign("statusList", $this->model->getStatusList());

        // 必须将结果集转换为数组
        $this->dayalist = Cache::get('dayalist');
        if (empty($this->dayalist)) {
            $dayalist = collection($this->model->order('timedate DESC')->select())->toArray();
            foreach ($dayalist as $k => &$v) {
                $v['name'] = __($v['name']);
            }
            Tree::instance()->init($dayalist);
            $this->dayalist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
            $ruledata = [0 => __('None')];
            foreach ($this->dayalist as $k => &$v) {
                $ruledata[$v['id']] = $v['name'];
                $v['name'] = str_replace(['&nbsp',';',' '],'',$v['name']);
            }
            Cache::set('dayalist',$this->dayalist,3600);
            $this->cache_info = 1;
        }else{
            $this->cache_info = 0;
        }
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $list = $this->dayalist;
            $total = count($this->dayalist);

            $filter = $this->request->get("filter", '');
            $filter = (array)json_decode($filter, true);
            $filter = $filter ? $filter : [];
            if (!empty($filter['timedate'])) {
                $timedate2 = explode(' - ',$filter['timedate']);
                $filter['timedates'][0] = strtotime($timedate2[0]);
                $filter['timedates'][1] = strtotime($timedate2[1]);
            }
            $lists = array();
            foreach ($list as $k=>$v) {
                $list[$k]['error'] = 0;
                if (!empty($filter['admin_id'])) {
                    if ((int)$filter['admin_id'] !== (int)$v['admin_id']) {
                        $list[$k]['error'] = 1;
                    }
                }
                if (!empty($filter['timedates'])) {
                    if ($filter['timedates'][1] >= $v['timedate'] && $filter['timedates'][0] <= $v['timedate']) {

                    }else{
                        $list[$k]['error'] = 1;
                    }
                }
                $admin = Db::name('admin')->where('id', '=', $v['admin_id'])->find();
                if($admin){
                    $list[$k]['admin_name'] = $admin['nickname'];
                }else{
                    $list[$k]['admin_name'] = '管理员未设置';
                }
            }

            $statistical['sales'] = 0;
            $statistical['a_tip'] = 0;
            $statistical['single_amount'] = 0;
            foreach ($list as $k=>$v) {
                if ($v['error'] !== 1) {
                    $lists[] = $v;
                    $statistical['sales'] += $v['salesz'];
                    $statistical['a_tip'] += $v['tip_price'];
                    $statistical['single_amount'] += $v['single'];
                }
            }
            $statistical['sales'] = round($statistical['sales'],2);
            $statistical['a_tip'] = round($statistical['a_tip'],2);
            $statistical['single_amount'] = round($statistical['single_amount'],2);
            $result = array("total" => $total, "rows" => $lists, "statistical" => $statistical, "cache_info" => $this->cache_info);
            return json($result);
        }
        return $this->view->fetch();
    }

    public function updateordertime()
    {
        $id = $this->request->param('ids');
        if(!empty($id)){
            //        更新每日汇总
            $Myshopify = new Myshopify();
            $return = $Myshopify->reportdaya($id);
            $this->success('更新成功','', $return);
        }else{
            $this->error('参数错误，请重新登录');
        }
    }

        /**
     * 启用
     */
    public function start($ids = '')
    {
         //        更新每日汇总
         $Myshopify = new Myshopify();
         $return = $Myshopify->reportdaya(0,2);
    }

}
